package com.mcz.yun4book.service.impl;


import com.mcz.yun4book.dao.mapper.TypeMapper;
import com.mcz.yun4book.entity.Type;
import com.mcz.yun4book.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;
    @Override
    public Page<Type> findAllType(int page, int limit, Type type) {
        if (StringUtils.isEmpty(type.getName())){
            type.setName("");
        }
        return typeMapper.findByNameContainingOrderByIdDesc(type.getName(), PageRequest.of(page-1,limit));
    }

    @Override
    public Type addType(Type type) {
        return typeMapper.save(type);
    }

    @Override
    public Type findTypeId(int id) {
        return typeMapper.getById(id);
    }

    @Override
    public Type editType(Type type) {
        return typeMapper.save(type);
    }

    @Override
    public int delTypeById(int id) {
        try {
        typeMapper.deleteById(id);
        } catch (Exception e){
            return 0;
        }return 1;
    }

    @Override
    public List<Type> findAllType() {
        return typeMapper.findAll();
    }

}
